/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.ui.controllers.exception.ControllerException;
import com.easoft.code.pos.ui.controllers.DataEntryController;
import com.easoft.code.pos.ui.controls.PreRenderControl;
import com.easoft.code.pos.ui.models.PojoTableModel;
import com.easoft.code.pos.util.UIUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Eliu
 */
public class FormDataEntry extends javax.swing.JPanel implements PreRenderControl {

    private DataEntryController actionController;
    private String menuName;
    private boolean editMode = false;

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public DataEntryController getActionController() {
        return actionController;
    }

    public void setActionController(DataEntryController actionController) {
        this.actionController = actionController;
    }

    public FormDataEntry(DataEntryController ac, String menuName) {
        try {
            initComponents();
            this.actionController = ac;
            this.menuName = menuName;

            this.actionController.configureUI(formArea, grid);
            this.grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    actionController.selectionListener(grid.getSelectedRow());
                    editMode = true;
                    btnDelete.setEnabled(editMode);
                    btnDelete.setEnabled(editMode);
                }
            });
            btnDelete.setEnabled(editMode);
            btnDelete.setEnabled(editMode);
        } catch (ControllerException ex) {
            Logger.getLogger(FormDataEntry.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates new form FormDataEntry
     */
    public FormDataEntry() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GridContainer = new javax.swing.JPanel();
        gridBarButtons = new javax.swing.JPanel();
        NewButton = new javax.swing.JButton();
        gridArea = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grid = new javax.swing.JTable();
        formContainer = new javax.swing.JPanel();
        formArea = new javax.swing.JPanel();
        controlsBar = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        ButtonOk = new javax.swing.JButton();
        ButtonKo = new javax.swing.JButton();

        setLayout(new java.awt.GridLayout(1, 0));

        GridContainer.setLayout(new java.awt.BorderLayout());

        NewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-16-new.png"))); // NOI18N
        NewButton.setText("Nuevo");
        NewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NewButtonActionPerformed(evt);
            }
        });
        gridBarButtons.add(NewButton);

        GridContainer.add(gridBarButtons, java.awt.BorderLayout.PAGE_START);

        gridArea.setLayout(new java.awt.BorderLayout());

        grid.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        grid.setRowHeight(20);
        grid.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(grid);

        gridArea.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        GridContainer.add(gridArea, java.awt.BorderLayout.CENTER);

        add(GridContainer);

        formContainer.setLayout(new java.awt.BorderLayout());
        formContainer.add(formArea, java.awt.BorderLayout.CENTER);

        controlsBar.setPreferredSize(new java.awt.Dimension(313, 35));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-16-delete.png"))); // NOI18N
        btnDelete.setText("Eliminar");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        controlsBar.add(btnDelete);

        ButtonOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tick.png"))); // NOI18N
        ButtonOk.setText("Guardar");
        ButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonOkActionPerformed(evt);
            }
        });
        controlsBar.add(ButtonOk);

        ButtonKo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel-red-button.png"))); // NOI18N
        ButtonKo.setText("Cancelar");
        ButtonKo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonKoActionPerformed(evt);
            }
        });
        controlsBar.add(ButtonKo);

        formContainer.add(controlsBar, java.awt.BorderLayout.PAGE_START);

        add(formContainer);
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonOkActionPerformed
        try {
            StringBuilder msg = new StringBuilder("Informacion ");
            if (editMode) {
                actionController.doUpdate(actionController.getSelRow());
                msg.append("actualizada ");
            } else {
                actionController.doSave(actionController.getSelRow());
                actionController.reload();
                msg.append("guardada ");
            }
            UIUtil.showMsg(msg.append("con exito!").toString(), menuName);
        } catch (ControllerException ex) {
            UIUtil.showError(ex.getMessage(), menuName);
        }
    }//GEN-LAST:event_ButtonOkActionPerformed

    private void NewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewButtonActionPerformed
        editMode = false;
        btnDelete.setEnabled(editMode);
        actionController.generateBlankForm();
    }//GEN-LAST:event_NewButtonActionPerformed

    private void ButtonKoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonKoActionPerformed
        this.formArea.removeAll();
        this.formArea.repaint();
    }//GEN-LAST:event_ButtonKoActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if (UIUtil.confirm("Desea eliminar registro?", menuName)) {
            try {
                actionController.doDelete(actionController.getSelRow());
                actionController.reload();
                ButtonKoActionPerformed(null);
                UIUtil.showMsg("Los datos se han eliminado!", menuName);
            } catch (ControllerException ex) {
                UIUtil.showError(ex.getMessage(), menuName);
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonKo;
    private javax.swing.JButton ButtonOk;
    private javax.swing.JPanel GridContainer;
    private javax.swing.JButton NewButton;
    private javax.swing.JButton btnDelete;
    private javax.swing.JPanel controlsBar;
    private javax.swing.JPanel formArea;
    private javax.swing.JPanel formContainer;
    private javax.swing.JTable grid;
    private javax.swing.JPanel gridArea;
    private javax.swing.JPanel gridBarButtons;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void refresh() {
        try {
            ((PojoTableModel) this.grid.getModel()).setData(this.actionController.getData());
            ((PojoTableModel) this.grid.getModel()).fireTableDataChanged();
        } catch (ControllerException ex) {
            ex.printStackTrace(System.err);
            System.out.println("error refrescanod...");
        }
    }
}
